package com.travel.service.tourist_demand;


import com.travel.common.ServerResponse;
import com.travel.dao.tourist_demand.TouristRecordsMapper;
import com.travel.dao.tourist_demand.TouristTrabelDemandMapper;
import com.travel.dao.user.TouristUserMapper;
import com.travel.pojo.tourist_demand.Address;
import com.travel.pojo.tourist_demand.TouristImage;
import com.travel.pojo.tourist_demand.TouristRecords;
import com.travel.pojo.tourist_demand.TouristTrabelDemand;
import com.travel.pojo.user.TouristUser;
import com.travel.utils.SortUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TouristTrabelDemandServer {

    @Autowired
    TouristTrabelDemandMapper trabelDemandMapper;

    @Autowired
    TouristUserMapper touristUserMapper;

    @Autowired
    TouristRecordsMapper touristRecordsMapper;


    /**
     * 根据子活动地名查找团
     * @param addressname 地名
     */
    public ServerResponse<List<TouristTrabelDemand>> queryTTlDemandByAddress(String addressname){
        int addressNubmer=trabelDemandMapper.getAddressByAddressName(addressname);
        List<TouristTrabelDemand> lists=trabelDemandMapper.getTlDemandByAddress(addressname);
        if (addressNubmer==0){
            return ServerResponse.createByErrorMessage("未找到您的需求");
        }else {
            return ServerResponse.createBySuccess(lists);
        }
    }


    /**
     * 游客查找团，根据code
     * @param code 入团码
     */
    public ServerResponse<TouristTrabelDemand> queryTTlDemandByCode(int code){
        System.out.println("正在使用code查团信息.....");
        TouristTrabelDemand trabelDemand= trabelDemandMapper.getTlDemandByCode(code);
        TouristTrabelDemand trabelDemand1=trabelDemandMapper.getUserAndTDemandByCode(code);
        //System.out.println("子活动数据："+trabelDemand.getAddress());
        if(trabelDemand!=null){
            List<Address> list=trabelDemand.getAddress();
            //使用starttime进行升序排序
            SortUtil.sortByWhat(list);
            trabelDemand.setAddress(list);
            System.out.println("查询成功...牛批嗷没死机");
            return ServerResponse.createBySuccess(trabelDemand);
        }else {
            System.out.println("查询成功...牛批嗷没死机");
            return ServerResponse.createBySuccess(trabelDemand1);
        }
    }

    /**
     * 游客查找团，根据id
     * @param tid 团id
     */
    public ServerResponse<TouristTrabelDemand> queryTTlDemandById(int tid){
        System.out.println("正在使用id查团信息.....奥里给");
        TouristTrabelDemand trabelDemand= trabelDemandMapper.getTlDemandById(tid);
        TouristTrabelDemand trabelDemand1=trabelDemandMapper.getTouristTrabelDemandById(tid);
        if (trabelDemand!=null){
            List<Address> list=trabelDemand.getAddress();
            //使用starttime进行升序排序
            SortUtil.sortByWhat(list);
            trabelDemand.setAddress(list);
            System.out.println("查询成功.....居然成功了嗷卧槽");
            return ServerResponse.createBySuccess(trabelDemand);
        }else {
            System.out.println("查询成功.....居然成功了嗷卧槽");
            return ServerResponse.createBySuccess(trabelDemand1);
        }
    }


    /**
     * 导游发布旅游团
     * @param userid 用户id
     * @param trabelDemand 旅游团基本信息
     */
    public ServerResponse<Map> addDemand(int userid,TouristTrabelDemand trabelDemand){
        TouristUser touristUser=touristUserMapper.queryById(userid);
        //修改用户类型
        trabelDemandMapper.updateUserTypeById1(userid);
        if (touristUser.getBean_number()<5){
            return ServerResponse.createByErrorMessage("您的U豆不足");
        }else {
            //生成随机入团码
            int code=(int)((Math.random()*9+1)*100000);
            trabelDemand.setCode(code);
            trabelDemand.setGuideid(userid);
            trabelDemand.setSingin(0);
            //查询对应城市图片
            //todo 下一次升级这里，图片不齐搞不起
            TouristImage img=trabelDemandMapper.queryImage(trabelDemand.getCity());
            if (img!=null){
                trabelDemand.setCityimg(img.getImgUrl());
            }else {
                trabelDemand.setCityimg("没有这个城市图片，你旅游个der");
            }
            //游客团主表插入数据，返回id
            trabelDemandMapper.insertTouristTrabelDemand(trabelDemand);
            int Ubean=touristUser.getBean_number()-5;
            //更新U豆数量
            touristUserMapper.reduceBeanNUmberById(userid,Ubean);
            //根据mybatis中selectKeyf返回的id向发布需求的用户表中添加tid
            trabelDemandMapper.updateTouristTrabelUser(userid,trabelDemand.getId());
            //放入集合返回给前端
            Map<String, Object> map=new HashMap<>();
            map.put("code",code);
            map.put("tid",trabelDemand.getId());
            map.put("bean_number",Ubean);
            System.out.println("建团成功->data:"+trabelDemand);
            return ServerResponse.createBySuccess("发布成功",map);
        }
    }

    /**
     * 删除旅游团
     * @param tid 团id
     */
    public ServerResponse<String> deleteTrabelDemand(int tid){
       int a=trabelDemandMapper.deleteTrabelDemandController(tid);
       if (a==1){
           return ServerResponse.createBySuccseeMessage("删除成功");
       }else {
           return ServerResponse.createByErrorMessage("删除失败");
       }
    }


    /**
     * 加入旅游团
     * @param userid 用户id
     * @param code 入团码
     */
    public ServerResponse<TouristTrabelDemand> joinDemand(int userid,int code){
        //修改用户类型
        trabelDemandMapper.updateUserTypeById2(userid);
        TouristTrabelDemand trabelDemand=trabelDemandMapper.getTouristTrabelDemandByCode(code);
        if (trabelDemand==null){
            return ServerResponse.createByErrorMessage("加入失败,请检查团号或者二维码是否清晰");
        }else {
            //添加进历史记录
            trabelDemandMapper.updateUserTidByUserId(userid, trabelDemand.getId());
            touristRecordsMapper.addTouristRecords(
                    new TouristRecords(
                            userid,
                            trabelDemand.getGuideid(),
                            trabelDemand.getId(),
                            trabelDemand.getStarttime(),
                            trabelDemand.getOvertime(),
                            trabelDemand.getTname(),
                            trabelDemand.getCity()));
            TouristTrabelDemand trabelDemand1=trabelDemandMapper.getUserAndTDemandByCode(code);
            System.out.println(trabelDemand1);
            return ServerResponse.createBySuccess("加入成功",trabelDemand1);
        }
    }

    /**
     *退出团
     */
    public ServerResponse<String> singOutDemand(int userid){
        int a=trabelDemandMapper.updateUserTidById(userid);
        if (a>0){
            return ServerResponse.createBySuccseeMessage("退出成功");
        }else {
            return ServerResponse.createByErrorMessage("退出失败");
        }
    }


    /**
     * 添加旅游团中的子活动,子活动签到默认关闭为0，活动默认未开启为0
     * @param address 旅游团子活动信息
     */
    public ServerResponse<Address> addActivity(Address address){
        address.setStartcode(0);
        trabelDemandMapper.addActivity(address);
        return ServerResponse.createBySuccess("发布成功",address);
    }

    /**
     *删除子活动
     * @param aid 子活动id
     */
    public ServerResponse<String> deleteActivity(int aid){
        int a= trabelDemandMapper.deleteActivity(aid);
        if (a==1){
            return ServerResponse.createBySuccseeMessage("删除成功");
        }
        else {
            return ServerResponse.createByErrorMessage("删除失败");
        }
    }


    /**
     * 开启活动   0未开始 ，1开始 ，2结束
     * @param aid 子活动唯一id
     */
    public ServerResponse<String> startActivity(int aid){
       int a= trabelDemandMapper.updateStartcodeOneByaid(aid);
       if (a>0){
           return ServerResponse.createBySuccseeMessage("开启成功");
       }else {
           return ServerResponse.createByErrorMessage("开启失败");
       }
    }

    /**
     *结束活动  0未开始 ，1开始 ，2结束
     * @param aid 子活动唯一id
     */
    public ServerResponse<String> overActivity(int aid){
       int a= trabelDemandMapper.updateStartcodeTwoByaid(aid);
        if (a>0){
            return ServerResponse.createBySuccseeMessage("活动结束");
        }else {
            return ServerResponse.createByErrorMessage("活动结束错误");
        }
    }

    /**
     * 开始签到
     *将团数据的singin更新为1，开启活动签到   0未开始签到，1开始签到，2签到结束
     * @param tid 子活动唯一id
     */
    public ServerResponse<String> startSingin(int tid){
       int a= trabelDemandMapper.updateSinginByOneBytid(tid);
        if (a>0){
            return ServerResponse.createBySuccseeMessage("开启签到成功");
        }else {
            return ServerResponse.createByErrorMessage("开启签到失败");
        }
    }

    /**
     *结束签到
     *将团数据的singin更新为2，活动签到结束   0未开始签到，1开始签到，2签到结束
     * @param tid 子活动唯一id
     */
    public ServerResponse<String> overSingin(int tid){
        int a=trabelDemandMapper.updateSinginByTwoBytid(tid);
        if (a>0){
            TouristTrabelDemand trabelDemand=trabelDemandMapper.getTouristTrabelDemandById(tid);
            System.out.println(trabelDemand);
            List<TouristUser> touristUser=trabelDemand.getTouristUsers();
                for(TouristUser list:touristUser ){
                    //结束签到，更新用户表签到状态为未签到
                    trabelDemandMapper.updateUserSinginOntById(list.getId());
                }
            return ServerResponse.createBySuccseeMessage("签到结束");
        }else {
            return ServerResponse.createByErrorMessage("签到结束错误");
        }
    }

    /**
     *将用户数据的singin更新为2，表示已签到  1未签到，2已签到
     * @param id 用户唯一id
     */
    public ServerResponse<String> yesSingin(int id){
        int a= trabelDemandMapper.updateUserSinginTwoById(id);
        if (a>0){
            return ServerResponse.createBySuccseeMessage("签到成功");
        }else {
            return ServerResponse.createByErrorMessage("签到错误");
        }
    }

    /**
     * 查团头图
     * @param cityName 城市名==照片名（imagename）
     */
    public ServerResponse<TouristImage> selectImageByCityName(String cityName){
       TouristImage touristImage= trabelDemandMapper.queryImage(cityName);
       if (touristImage==null){
           return ServerResponse.createByErrorMessage("无此照片");
       }else {
           return ServerResponse.createBySuccess(touristImage);
       }
    }


}
